package com.qf.lja.dao;

import com.qf.lja.enity.Address;
import com.qf.lja.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddrDao {

    public List<Address> selectAddr(Integer userId) {
        String sql = "select * from buyr_address where user_id = ? order by `defalut_addr` desc";

        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql, userId);
        List<Address> list = new ArrayList<>();
        try {
            while (rs.next()) {
                list.add(convertToProduct(rs));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public Address convertToProduct(ResultSet rs) {
        Address address = new Address();
        try {
            address.setAddressId(rs.getInt("address_id"));
            address.setUserId(rs.getInt("user_id"));
            address.setRecipients(rs.getString("recipients"));
            address.setPhone(rs.getString("phone"));
            address.setAddrName(rs.getString("addr_name"));
            address.setDefalutAddr(rs.getInt("defalut_addr"));
            return address;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return address;
    }


    public void addAddr(String recipients, String phone, String addrName, Integer userId) {
        String sql = "insert into buyr_address(user_id, recipients, phone, addr_name, `defalut_addr`)" +
                " values(?,?,?,?,?)";
        JDBCUtil jdbcUtil = new JDBCUtil();
        jdbcUtil.update(sql, userId, recipients, phone, addrName, 0);
        jdbcUtil.closeConnection();
    }

    public void deleteAddr(Integer addressId) {
        String sql = "delete from buyr_address where address_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        jdbcUtil.update(sql, addressId);
        jdbcUtil.closeConnection();
    }

    public void setAddrToDefault(Integer addressId) {
        String sql1 = "update buyr_address set `defalut_addr` = 1 where `address_id` = ?";
        String sql2 = "update buyr_address set `defalut_addr` = 0 where `address_id` != ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        jdbcUtil.update(sql1, addressId);
        jdbcUtil.update(sql2, addressId);
        jdbcUtil.closeConnection();
    }

    //查询默认地址id
    public Integer getDefaultAddrId(Integer userId) {
        String sql = "select address_id from buyr_address where user_id = ? and `defalut_addr` = 1";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql, userId);
        try {
            if (rs.next()) {
                return rs.getInt("address_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updateAddr(Integer aId, String aName, String aPhone, String aDetail) {
        String sql = "update buyr_address set addr_name = ?, phone = ?, recipients = ? where address_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        jdbcUtil.update(sql, aDetail, aPhone, aName, aId);
        jdbcUtil.closeConnection();
    }

    public Address findById(Integer addressId) {
        String sql = "select * from buyr_address where address_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet rs = jdbcUtil.query(sql, addressId);
        try {
            if (rs.next()) {
                return convertToProduct(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
